package com.guigu.www.realm;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guigu.www.pojo.Users;
import com.guigu.www.service.UsersService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;


public class MyRealm extends AuthorizingRealm {
    //获取service
    @Autowired
    UsersService usersService;
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
       return null;
    }


    //登录验证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        //在token中获取账号
        String loginid= (String) token.getPrincipal();
        //根据账号查询当前用户
        QueryWrapper<Users> queryWrapper=new QueryWrapper<Users>();
        queryWrapper.eq("LOGIN_ID",loginid);
        Users usernoe = usersService.getOne(queryWrapper);
        System.out.println(usernoe);
        //判断是否拥有该账号
        if(usernoe==null){
            throw  new AuthenticationException("账号不存在");
        }else if(usernoe.getStatus()==1){
            throw new AuthenticationException("账号已禁用");
        }
        //获取认证器
        //令牌  密码  盐  字符串
        SimpleAuthenticationInfo simpleAuthenticationInfo;
        simpleAuthenticationInfo = new SimpleAuthenticationInfo(usernoe,usernoe.getPassword(),"MyRealm");
        return simpleAuthenticationInfo;
    }



}
